package com.yuncheng.oaApi.controller;

import java.math.BigDecimal;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.yuncheng.oaApi.constant.NkConstant;
import com.yuncheng.oaApi.entity.NkRsXmyszbb;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.NkRsXmyszbbMapper;
import com.yuncheng.oaApi.service.INkRsXmyszbbService;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.NkRsYstpmx;
import com.yuncheng.oaApi.service.INkRsYstpmxService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 内控-预算调配明细
 * @Author: lhb
 * @Date: 2023-11-21
 * @Version: V1.0
 */
@Api(tags = "NK_内控-预算调配明细接口")
@RestController
@RequestMapping("/api/spzx-oa/nkRsYstpmx")
public class NkRsYstpmxController extends BaseController<NkRsYstpmx, INkRsYstpmxService> {

    private static final Logger log = LoggerFactory.getLogger(NkRsYstpmxController.class);

    @Resource
    private INkRsYstpmxService nkRsYstpmxService;

    @Resource
    @Lazy
    private INkRsXmyszbbService nkRsXmyszbbService;

    @Resource
    @Lazy
    private NkRsXmyszbbMapper nkRsXmyszbbMapper;

    /**
     * 查询拆分后的项目预算指标
     * 全部门
     */
    @AutoLog(value = "查询拆分后的项目预算指标")
    @ApiOperation(value = "查询拆分后的项目预算指标")
    @GetMapping(value = "/xmyslist")
    public HttpResult<?> xmYsList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                  @RequestParam(value = "sfcxbbm",defaultValue = "false") boolean sfcxbbm){
        NkRsXmyszbb xmyszbb = new NkRsXmyszbb();
        Page<NkRsXmyszbb> page = new Page<>(pageNo,pageSize);
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        //如果查询本部门 为 true 则查本部门的 否则就查状态为启用的
        if (sfcxbbm){
            xmyszbb.setFbmid(currentUser.getDeptId());
        }
        xmyszbb.setFyszbzt(NkConstant.ZBZT_QY);

        Page<NkRsXmyszbb> pageList = nkRsXmyszbbMapper.findKyXmyxPageList(page, xmyszbb);
        return HttpResult.ok(pageList);
    }


    /**
     * 分页列表查询
     *
     * @param nkRsYstpmx
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "内控-预算调配明细-分页列表查询")
    @ApiOperation(value = "内控-预算调配明细-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(NkRsYstpmx nkRsYstpmx, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<NkRsYstpmx> queryWrapper = QueryBuilder.initQueryWrapper(nkRsYstpmx, req.getParameterMap());
        Page<NkRsYstpmx> page = new Page<>(pageNo, pageSize);
        IPage<NkRsYstpmx> pageList = nkRsYstpmxService.page(page, queryWrapper);
        for(NkRsYstpmx vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }



    /**
     * 添加
     *
     * @param nkRsYstpmx
     * @return
     */
    @AutoLog(value = "内控-预算调配明细-添加")
    @ApiOperation(value = "内控-预算调配明细-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkRsYstpmx nkRsYstpmx) {
        HttpResult<String> result = new HttpResult(true);
        //保存前判断 拨出的金额 和 项目预算指标的结余金额 的判断

        if (nkRsYstpmx.getFtplx().equals(NkConstant.YSTPMXLX_BC)){
            NkRsXmyszbb rsXmyszbb = nkRsXmyszbbService.getById(nkRsYstpmx.getFxmyszbid());

            BigDecimal zbjyDecimal = new BigDecimal(rsXmyszbb.getFzbjy());
            BigDecimal tpjeDecimal = new BigDecimal(nkRsYstpmx.getFtpje());

            int i = tpjeDecimal.compareTo(zbjyDecimal);
            if (i >0){
                throw new BusinessException("当前拨出金额，大于选择的项目预算指标，请重新分配，或者选择其他项目拨出");
            }
        }

        nkRsYstpmxService.save(nkRsYstpmx);
        result.setResult(nkRsYstpmx.getId());
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 编辑
     *
     * @param nkRsYstpmx
     * @return
     */
    @AutoLog(value = "内控-预算调配明细-编辑")
    @ApiOperation(value = "内控-预算调配明细-编辑")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody NkRsYstpmx nkRsYstpmx) {
        nkRsYstpmxService.updateById(nkRsYstpmx);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-预算调配明细-通过id删除")
    @ApiOperation(value = "内控-预算调配明细-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        nkRsYstpmxService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "内控-预算调配明细-批量删除")
    @ApiOperation(value = "内控-预算调配明细-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        nkRsYstpmxService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-预算调配明细-通过id查询")
    @ApiOperation(value = "内控-预算调配明细-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        NkRsYstpmx nkRsYstpmx = nkRsYstpmxService.getById(id);
        return HttpResult.ok(nkRsYstpmx);
    }

    /**
     * 数据转换
     * @param nkRsYstpmx
     */
    private void formatValue(NkRsYstpmx nkRsYstpmx){
    }
}
